package com.scatteredServer.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.scatteredServer.common.Result;
import com.scatteredServer.mapper.FeedbackMapper;
import com.scatteredServer.pojo.entity.Feedback;
import com.scatteredServer.pojo.excel.FeedbackExcel;
import com.scatteredServer.pojo.form.add.AddFeedback;
import com.scatteredServer.pojo.form.update.UpdateRoughlyPosting;
import com.scatteredServer.pojo.query.FeedbackQuery;
import com.scatteredServer.pojo.vo.BaseVo;
import com.scatteredServer.pojo.vo.FeedbackVo;
import com.scatteredServer.service.impl.FeedbackServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("s4/feedback")
@Slf4j
@Tag(name = "意见反馈管理", description = "意见反馈管理")
public class FeedbackController {

    @Autowired
    FeedbackMapper feedbackMapper;

    @Autowired
    FeedbackServiceImpl  feedbackService;

    @Operation(summary = "添加反馈", description = "添加反馈")
    @PostMapping("/add")
    public Result<String> add(@RequestBody AddFeedback addFeedback) {
        Feedback feedback = new Feedback();
        BeanUtil.copyProperties(addFeedback,feedback);
        feedbackMapper.insert(feedback);
        return Result.success("添加成功");
    }

    @Operation(summary = "删除反馈", description = "添加反馈")
    @DeleteMapping("/deleteByIds")
    @Transactional
    public Result<String> deleteByIds(@RequestBody List<Integer> list) {
        feedbackMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Operation(summary = "获取反馈", description = "添加反馈")
    @PostMapping("/getList")
    public Result<BaseVo<FeedbackVo>> getList(@RequestBody FeedbackQuery feedbackQuery) {
        return feedbackService.getList(feedbackQuery);
    }

    @SneakyThrows
    @Operation(summary = "导出反馈", description = "导出反馈")
    @PostMapping("/exportInfo")
    public void exportInfo(@RequestBody FeedbackQuery feedbackQuery, HttpServletResponse response) {

        //构造下载前缀
        String prefixName="";
        Integer postingId = feedbackQuery.getPostingId();
        String teacherName = feedbackQuery.getTeacherName();
        String feedbackContext = feedbackQuery.getFeedbackContext();
        if(postingId != null){
            prefixName+=postingId;
        }else {
            return ;
        }
        if(teacherName!=null&&!teacherName.equals("")){
            prefixName+=teacherName;
        }
        if(feedbackContext!=null&&!feedbackContext.equals("")){
            prefixName+=feedbackContext;
        }
        prefixName+="相关";

        List<FeedbackExcel> feedbackExcelList= feedbackMapper.exportInfo(feedbackQuery);
        EasyExcel.write(response.getOutputStream())
                .head(FeedbackExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(prefixName+"导出反馈表")
                .doWrite(feedbackExcelList);
    }


}
